package cn.tedu._07springaop.egaop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

import java.util.Arrays;

@Aspect
@Component
public class CartAspect {

//    @Before(value = "execution(public boolean cn.tedu._07springaop.egaop.CartServiceImpl.*(..))")
//    public void beforeMethod(JoinPoint joinPoint){
//        System.out.println("开始添加购物车");
//
//    }
//
//    @AfterReturning(value = "execution(public boolean cn.tedu._07springaop.egaop.CartServiceImpl.*(..))",returning = "r")
//    public void afterReturningMethod(JoinPoint joinPoint , boolean r) {
//        String name = joinPoint.getSignature().getName();
//        String goods = Arrays.toString(joinPoint.getArgs());
//        if (r) {
//            System.out.println(name + "添加" + goods + "购物车成功");
//        } else {
//            System.out.println(name + "添加" + goods + "购物车失败");
//        }
//    }
//
//    @After(value = "execution(public boolean cn.tedu._07springaop.egaop.CartServiceImpl.*(..))")
//    public void afterMethod(JoinPoint joinPoint){
//        System.out.println("操作完成");
//
//    }

    @Around(value = "execution(public boolean cn.tedu._07springaop.egaop.CartServiceImpl.*(..))")
    public boolean aroundMethod(ProceedingJoinPoint joinPoint){
        boolean result;
        try {
            System.out.println("开始添加购物车");
            result = (boolean)joinPoint.proceed();
            String name = joinPoint.getSignature().getName();
            String goods = Arrays.toString(joinPoint.getArgs());
            if (result) {
                System.out.println(name + "添加" + goods + "购物车成功");
            } else {
                System.out.println(name + "添加" + goods + "购物车失败");
            }
        } catch (Throwable e) {
            throw new RuntimeException(e);
        }finally {
            System.out.println("操作完成");

        }

        return result;

    }



}
